<?
session_start();
include_once("database.php");
include_once("backend.php");
include_once("login_management.php");
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"><!-- InstanceBegin template="/Templates/Scouting.dwt.php" codeOutsideHTMLIsLocked="false" -->
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<!-- InstanceBeginEditable name="doctitle" -->
<title>Input | Team Spyder Scouting</title>
<!-- InstanceEndEditable -->
<link rel="stylesheet" type="text/css" href="scouting.css"/>
<!-- InstanceBeginEditable name="head" -->
<!-- InstanceEndEditable -->
</head>

<body>
<div class="main">
<div class="top"><l>
<a class="navigation" href="./index.php">Home</a> | 
<a class="navigation" href="./input.php">Input Data</a> | 
<a class="navigation" href="./view.php">View Data</a> | 
<a class="navigation" href="./edit.php">Edit Data</a> | 
<a class="navigation" href="./predict.php">Match Predictor</a> | 
<script type="text/javascript">
function LoadLogin()
{
var xmlhttp;
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("content").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","login.php",true);
xmlhttp.send();
}
</script>
<?
if($logged_in)
{
	echo "<l> Welcome, " . $_SESSION['username'] . " </l>";
	echo "<a class=\"navigation\" href=\"./logout.php\">Log out</a>\n";
}
else
{
	echo "<l> You are not logged in. </l>";
	echo "<a class=\"navigation\" href=\"#\" onclick=\"LoadLogin()\">Log in</a>\n";
}

?>
</l>
</div>
<div id="content" class="content"><!-- InstanceBeginEditable name="Content" -->
<?
if($logged_in)
{
	$fix=false;
	if(isset($_POST['stage']))
	{
		$table=GetDataTable();
		$teams=$_POST['teams'];
		$match=$_POST['Match_1'];
		$regional=$_SESSION['regional'];
		$colnames=EnumerateColumnNames();
		$fields=count($colnames);
		/*for($k=1;$k<=$teams;$k++)
		{
			for($l=1;$k<=$fields;$l++)
			{
				echo $colnames[$l].'_'.$k;
				if($colnames[$l]=="")
				{
					continue;
				}
				if($colnames[$j]=="Match")
				{
					if(!isset($_POST['Match_1']))
				{
					die("You have not filled out all of the required fields");
				}
					continue;
				}
				else{
				// check variables exist for all of em... if not exist, die;
				if(!isset($_POST[urlencode($colnames[$l]).'_'.$k]))
				{
					die("You have not filled out all of the required fields");
				}}
			}
		}*/
		$query9="SELECT * FROM `" . $table . "` WHERE Regional='" . $_SESSION['regional'] . "' AND `Match`='" . $match . "'";
		#echo $query9;
		$result=mysql_query($query9) or die(mysql_error());
		if(mysql_num_rows($result)!=0)
		{
			echo "<p>You have already entered match number " . $match . ".  Please try again.</p>";
		}
		else
		{
		
		for($t=1;$t<=$teams;$t++)
		{
			$checkquery="SELECT * FROM `teams` WHERE Regional='".$_SESSION['regional']."' AND Datatable='" . $table . "' AND Team='".$_POST['Team_'.$t]."'";
			$result8=mysql_query($checkquery) or die(mysql_error());
			if(mysql_num_rows($result8)==0)
			{
				//print("Team does not exist: Team " . $_POST['Team_'.$t]);
				$fix=true;
			}
		}
		
		for($i=1;$i<=$teams;$i++)
		{
			$query="INSERT INTO `" . $table . "` VALUES ('" . $regional . "', ";
			//print $query . "<br />";
			for($j=1;$j<=$fields;$j++)
			{
				if($i!=1 && $colnames[$j]=="Match")
				{
					$query= $query . "'" . $_POST[$colnames[$j] . '_1'] . "'";
					if($j!=$fields)
					{
						$query= $query . ", ";
					}
					else
					{
						$query= $query . ");";
					}
					continue;
				}
				$query= $query . "'" . $_POST[urlencode($colnames[$j]) . '_' . $i] . "'";
				if($j!=$fields)
				{
					$query= $query . ", ";
				}
				else
				{
					$query= $query . ");";
				}
				//print $_POST['' . ($colnames[$j] . '_' . $i) . ''] . "<br />";
				//print $query . "<br />";
			}
			//print $query . "<br />";
			//check time
			$checkquery="SELECT * FROM `teams` WHERE Regional='".$_SESSION['regional']."' AND Datatable='" . $table . "' AND Team='".$_POST['Team_'.$i]."'";
			$result8=mysql_query($checkquery) or die(mysql_error());
			if(mysql_num_rows($result8)==0)
			{
				print("Team does not exist: Team " . $_POST['Team_'.$i]);
				$fix=true;
			}
			else if($fix==false)
			{
				$result=mysql_query($query) or die(mysql_error());
				        echo "<p>Your data has been submitted.</p>";
			}
		}
		}
	}
	DisplayInputDataForm($fix);
	mysql_close();
}
else
{
	displayLogin();
	mysql_close();
}
?>
<!-- InstanceEndEditable -->
</div>
</div>

</body>
<!-- InstanceEnd --></html>
